<?php
  error_reporting(E_ALL^E_NOTICE); //取消警告显示
  header('Content-type:application/json');
  date_default_timezone_set("Asia/Shanghai");
  include $_SERVER['DOCUMENT_ROOT']."/common/fun.php";
  include "../conn.php";//https

  $userid = get_post_para('userid',true);
  $deviceid = get_post_para('deviceid',true);
  // 判断用户是否存在
  check_userid($userid,$con);
  $name = get_post_para('name',true);
  $type = get_post_para('type',true);

  //查询模块数量
  $moduleid = 1;
  $result = mysqli_query($con, "SELECT count(*) FROM device_$type WHERE userid = '$userid' and deviceid = '$deviceid'");
  if($result)
  {
    $row = mysqli_fetch_array($result);
    $moduleid = $row[0]+1;
  }
  
  //存储
  $sql_insert = "insert device_$type (userid,deviceid,moduleid,name,created) values('$userid','$deviceid',$moduleid,'$name',now())";
  $res_insert = mysqli_query($con,$sql_insert);
  if ($res_insert){
    $myArray["resault"] = 'success';
  } 
  else{
    $myArray["msg"]=mysqli_error($con);
    $myArray["resault"] = 'fail';
  }           

  // mysqli_close($con);
  // print_r($myArray); 
  $json = json_encode($myArray,JSON_UNESCAPED_UNICODE);
  echo $json;
?>